package com.pension.servlet.room;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pension.model.dao.RoomDao;
import com.pension.model.dto.Room;

@WebServlet("/room/view.action")
public class RoomViewServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		//1. 요청 정보에서 조회할 방이름를 읽고 변수에 저장
		//   (방이름이 없으면, 목록으로 이동)
		String name = req.getParameter("roomname");
		if (name == null || name.length() == 0) {
			resp.sendRedirect("/pension/room/list.action");
			return;
		}

		// 2. 1에서 읽은 아이디로 데이터베이스에서 조회
		// (조회된 데이터가 없으면 목록으로 이동)
		RoomDao dao = new RoomDao();
		Room room = dao.getRoomByName(name);
		if (room == null) {
			resp.sendRedirect("/pension/room/list.action");
			return;
		}

		// Servlet에서 처리된 데이터를 JSP에 전달 (request 객체 이용)
		req.setAttribute("room", room);

		// 3-2. 응답 컨텐츠 생성 (JSP 이용해서 처리 - jsp page로 forward)
		RequestDispatcher dispatcher = req
				.getRequestDispatcher("/WEB-INF/views/room/view.jsp");

		dispatcher.forward(req, resp);

	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException ,IOException {
		req.setCharacterEncoding("utf-8");
		doGet(req, resp);
	};

}








